#include "expression_transition.h"
#include "expression_tree.h"
#include "skeleton.h"

using namespace krryn::animation;

expression_transition::expression_transition(expression_tree *a_Tree)
	: expression(2), 
		m_Tree(a_Tree), 
		m_Duration(1.f), 
		m_CurrentTime(0)
{
}

void expression_transition::blend(float a_DT, skeleton *a_Result){
	m_CurrentTime += a_DT;

	if(m_CurrentTime < m_Duration){
		skeleton *a_Scratch = m_Tree->request_scratch_skeleton();

		get_input(0)->blend(a_DT, a_Scratch);
		get_input(1)->blend(a_DT, a_Result);

		float a_Alpha = m_CurrentTime / m_Duration;
		skeleton::interpolate(a_Result, a_Scratch, a_Result, a_Alpha);

		m_Tree->return_scratch_skeleton(a_Scratch);
	}else{
		get_input(1)->blend(a_DT, a_Result);
		//get_output()->replace(this, get_input(1));
		// TODO: We should /really/ delete this node, and 'last', 
		//  from the tree and swap it with 'next'
	}
}